package com.jui.material.widgets;

import android.content.Context;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jui.material.R;
import com.jui.material.views.ButtonFlat;

public class EditDialog extends android.app.Dialog {

  Context context;
  View view;
  View backView;
  String message;
  TextView messageTextView;
  String title;
  TextView titleTextView;
  Drawable drawable = null;
  ImageView iconView;
  String inputHint = null;
  EditText inputView;

  ButtonFlat buttonAccept;
  ButtonFlat buttonCancel;

  String buttonCancelText;

  View.OnClickListener onAcceptButtonClickListener;
  View.OnClickListener onCancelButtonClickListener;
  boolean mCancelable = true;


  public EditDialog(Context context, String title, String message) {
    super(context, android.R.style.Theme_Translucent);
    this.context = context;// init Context
    this.message = message;
    this.title = title;

  }

  public EditDialog(Context context, String title, String message, Drawable drawable) {
    super(context, android.R.style.Theme_Translucent);
    this.context = context;// init Context
    this.message = message;
    this.title = title;
    this.drawable = drawable;
  }

  public void addCancelButton(String buttonCancelText) {
    this.buttonCancelText = buttonCancelText;
  }

  public void addCancelButton(String buttonCancelText,
      View.OnClickListener onCancelButtonClickListener) {
    this.buttonCancelText = buttonCancelText;
    this.onCancelButtonClickListener = onCancelButtonClickListener;
  }


  @Override
  protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.edit_dialog);

    view = (RelativeLayout) findViewById(R.id.contentDialog);
    backView = (RelativeLayout) findViewById(R.id.dialog_rootView);
    backView.setOnTouchListener(new OnTouchListener() {

      @Override
      public boolean onTouch(View v, MotionEvent event) {
        if (mCancelable) {
          if (event.getX() < view.getLeft() || event.getX() > view.getRight()
              || event.getY() > view.getBottom() || event.getY() < view.getTop()) {
            dismiss();
          }
        }
        return false;
      }
    });

    //icon
    this.iconView = (ImageView) findViewById(R.id.icon);
    if (drawable == null) {
      iconView.setVisibility(View.GONE);
    } else {
      iconView.setVisibility(View.VISIBLE);
      iconView.setImageDrawable(drawable);
    }

    //title
    this.titleTextView = (TextView) findViewById(R.id.title);
    if (title == null) {
      titleTextView.setVisibility(View.GONE);
    } else {
      titleTextView.setVisibility(View.VISIBLE);
      titleTextView.setText(title);
    }

    //message
    this.messageTextView = (TextView) findViewById(R.id.message);
    if (message == null) {
      messageTextView.setVisibility(View.GONE);
    } else {
      messageTextView.setVisibility(View.VISIBLE);
      messageTextView.setText(message);
    }
    
    //input
    this.inputView = (EditText) findViewById(R.id.input);
    if (inputHint!=null) {
      inputView.setHint(inputHint);
    }

    //positive button
    this.buttonAccept = (ButtonFlat) findViewById(R.id.button_accept);
    buttonAccept.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        dismiss();
        if (onAcceptButtonClickListener != null) onAcceptButtonClickListener.onClick(v);
      }
    });

    //negative button
    if (buttonCancelText != null) {
      this.buttonCancel = (ButtonFlat) findViewById(R.id.button_cancel);
      this.buttonCancel.setVisibility(View.VISIBLE);
      this.buttonCancel.setText(buttonCancelText);
      buttonCancel.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
          dismiss();
          if (onCancelButtonClickListener != null) onCancelButtonClickListener.onClick(v);
        }
      });
    }
    
    inputView.addTextChangedListener(new TextWatcher() {
      
      @Override
      public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (s==null || s.toString().trim().equals("")) {
          buttonAccept.setEnabled(false);
        }else {
          buttonAccept.setEnabled(true);
        }
        
      }
      
      @Override
      public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        if (s==null || s.toString().trim().equals("")) {
          buttonAccept.setEnabled(false);
        }else {
          buttonAccept.setEnabled(true);
        }
        
      }
      
      @Override
      public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub
        
      }
    });

    resizeWindow(0);
  }

  public void resizeWindow(int width) {
    WindowManager m = getWindow().getWindowManager();
    Display d = m.getDefaultDisplay();
    Point size = new Point();
    d.getSize(size);
    RelativeLayout.LayoutParams params =
        (android.widget.RelativeLayout.LayoutParams) view.getLayoutParams();
    if (width==0) {
      if (size.x < size.y) {//vertical
        params.width = (int) (size.x * 0.75);
      }else {
        params.width = (int) (size.x * 0.55);
      }
    }else {
      params.width = width;
    }
    params.addRule(RelativeLayout.CENTER_IN_PARENT);
    view.setLayoutParams(params);
  }

  @Override
  public void show() {
    super.show();
    // set dialog enter animations
    view.startAnimation(AnimationUtils.loadAnimation(context, R.anim.dialog_main_show_amination));
    backView.startAnimation(AnimationUtils.loadAnimation(context, R.anim.dialog_root_show_amin));
  }
  
  @Override
  public void setCancelable(boolean flag) {
    // TODO Auto-generated method stub
    super.setCancelable(flag);
    mCancelable = flag;
  }

  // GETERS & SETTERS

  //icon
  public void setIcon(Drawable drawable) {
    this.drawable = drawable;
  }
//title
  public String getTitle() {
    return title;
  }

  public void setTitle(String title) {
    this.title = title;
  }

  public TextView getTitleTextView() {
    return titleTextView;
  }

  public void setTitleTextView(TextView titleTextView) {
    this.titleTextView = titleTextView;
  }
  //message
  public String getMessage() {
    return message;
  }

  public void setMessage(String message) {
    this.message = message;
  }

  public TextView getMessageTextView() {
    return messageTextView;
  }

  public void setMessageTextView(TextView messageTextView) {
    this.messageTextView = messageTextView;
  }

  //input
  public void setInputHint(String hint){
    this.inputHint = hint;
  }
  
  public String getInput(){
    if (inputView!=null) {
      return inputView.getText().toString().trim();
    }
    return null;
  }
  
  public EditText getEditText(){
    return inputView;
  }
  public void setEditText(EditText editText){
    this.inputView = editText;
  }

  public ButtonFlat getButtonAccept() {
    return buttonAccept;
  }

  public void setButtonAccept(ButtonFlat buttonAccept) {
    this.buttonAccept = buttonAccept;
  }

  public ButtonFlat getButtonCancel() {
    return buttonCancel;
  }

  public void setButtonCancel(ButtonFlat buttonCancel) {
    this.buttonCancel = buttonCancel;
  }

  public void setOnAcceptButtonClickListener(View.OnClickListener onAcceptButtonClickListener) {
    this.onAcceptButtonClickListener = onAcceptButtonClickListener;
    if (buttonAccept != null) buttonAccept.setOnClickListener(onAcceptButtonClickListener);
  }

  public void setOnCancelButtonClickListener(View.OnClickListener onCancelButtonClickListener) {
    this.onCancelButtonClickListener = onCancelButtonClickListener;
    if (buttonCancel != null) buttonCancel.setOnClickListener(onCancelButtonClickListener);
  }


  @Override
  public void dismiss() {
    Animation anim = AnimationUtils.loadAnimation(context, R.anim.dialog_main_hide_amination);
    anim.setAnimationListener(new AnimationListener() {

      @Override
      public void onAnimationStart(Animation animation) {}

      @Override
      public void onAnimationRepeat(Animation animation) {}

      @Override
      public void onAnimationEnd(Animation animation) {
        view.post(new Runnable() {
          @Override
          public void run() {
            EditDialog.super.dismiss();
          }
        });

      }
    });
    Animation backAnim = AnimationUtils.loadAnimation(context, R.anim.dialog_root_hide_amin);

    view.startAnimation(anim);
    backView.startAnimation(backAnim);
  }



}
